Python is a high-level interpreted programming language.
high level = there are many things that come with python that you (as a developer) do not have to code from scratch
interpreted = you are a scientist, not an engineer, you want to be able to interact with your data, your program, or your script without having to compile it before being presented results, gone are the days of pode/compile/pray/test/code/re-compile, you can write 'throw away' scrips and move on.
it's free
it's extensiable
In [ ]:
# this is a python comment, it spans 1 line, and 1 line only
'''
this is not a multi-line comment
you use 3 single quotes
this is also called a docstring.
this is what is printed out when you call help
'''
"""
you can also use
3 double quotes
"""
In [ ]:
1 + 1 #2
9 - 1 # 8
2 * 3 #6
2**10 #1024
8/4 #2
9/2 #4
9/2.0 #4.5
9 + 2 * 3
(9 + 2) * 3
In [3]:
Out[3]:
In [5]:
pizza = 3
to see the value of a variable we can either just put the variable on a separate line at the end (ipython specific function) or tell python to print the variable
In [9]:
x
Out[9]:
In [12]:
print x # python 2.7
In [13]:
print(x) #python 2.7 or 3+
Variables can be anything you can think about:
more built-in types:
In [14]:
x = True
y = False
z = 3
a = 2.7
b = 'welcome to the csaph python intro'
print x, y, z, a, b
Python is a dynamically typed language
variables can be re-used and re-assigned on-the-fly
In [ ]:
x = True
print x
# reverse inequalities by using 'not'
print not x
x = False
print x
x = 3
print x
x = 3.14
print x
x = "hello!"
print x
Sometimes when you are working with a dataset (or any piece of data) you will want to make comparisions.
For example, you would like to know if: $$ \text{age} > 25 $$
We can do this and other forms of comparisons and inequalities in python as well (think back to your early math days!)
In [15]:
30 > 25 #true
25 > 30 #false
# assign some values to some variables
age1 = 25
age2 = 30
age1 > age2 # false
age1 <= age2 # true
age1 = age2 # false
age1 != age2 # true
3.0 == 3 # true
"hello" == "Hello" #false
Out[15]:
In [ ]:
# this is a string
"Hello"
# this is also a string
'Hello'
# it does not matter if you use " or ', just be consistent
In [ ]:
# concatenation
"Hello" + "everyone" + "attending"
"hello " + "everyone"
"hello" + " everyone"
In [ ]:
In [16]:
# print the number 8
(6 - 4) * 4
Out[16]:
In [19]:
# print the number 102
( 4+ 6) * (4 + 6) + (6 - 4)
str(6+4) + str(2)
Out[19]:
In [20]:
# print the string "Tis but a flesh wound"
'This but a ' + 'flesh wound'
Out[20]:
In [21]:
# print the boolean value True
6 > 4
Out[21]: